package com.zhida.smartcontrol.dialog.classifydialog;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jay.tools.recycler.OnItemClick;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.mvp.MVPBaseDialogFragment;
import com.zhida.smartcontrol.common.bean.DeviceClassifyBean;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: tanjianpei
 * date: 2019/12/16 016 16:50
 * description: 选择分类
 */
public class ClassifyDialogFragment extends MVPBaseDialogFragment<ClassifyDialogContract.View, ClassifyDialogPresenter> implements ClassifyDialogContract.View {
    @BindView(R.id.recycler_view)
    RecyclerView mRView;

    private ClassifyAdapter mAdapter;
    private DeviceClassifyBean mBean;

    public static ClassifyDialogFragment newInstance() {
        Bundle args = new Bundle();
        ClassifyDialogFragment fragment = new ClassifyDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        mShowBottom = true;
        return R.layout.dialog_common_sel;
    }

    @Override
    protected void initDatas(Bundle savedInstanceState) {
        mRView.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new ClassifyAdapter(null);
        mAdapter.bindToRecyclerView(mRView);

        mPresenter.buildClassify();
    }

    @OnClick({R.id.btn_cancel, R.id.btn_confirm})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_confirm:
                if (null != mListener) {
                    mListener.onDestroy(mBean, false);
                    dismiss();
                }
                break;
        }
    }

    @Override
    protected void initEvents(Dialog dialog) {
        mAdapter.setClickListener(new OnItemClick<DeviceClassifyBean>() {
            @Override
            public void onClick(DeviceClassifyBean info, int tag) {
                mBean = info;
            }
        });
    }

    @Override
    public void showData(List<DeviceClassifyBean> data) {
        mAdapter.replaceData(data);
        mBean = data.get(0);
    }
}
